<?php

namespace common\models\navigation;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%navigation_item}}".
 *
 * @property int $id
 * @property int $navigation_id 导航ID
 * @property string $title 标题
 * @property string|null $name 标识名称
 * @property string|null $description 描述信息
 * @property string|null $thumb 缩略图
 * @property string|null $icon_class 图标
 * @property string|null $color 颜色值
 * @property int $listorder 排序
 * @property string|null $link 链接地址
 * @property string|null $mark 备注
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class NavigationItem extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%navigation_item}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['navigation_id', 'listorder', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title'], 'required'],
            [['title', 'name'], 'string', 'max' => 50],
            [['description'], 'string', 'max' => 100],
            [['thumb', 'icon_class', 'link', 'mark'], 'string', 'max' => 255],
            [['color'], 'string', 'max' => 7],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'navigation_id' => '导航ID',
            'title' => '标题',
            'name' => '标识名称',
            'description' => '描述信息',
            'thumb' => '缩略图',
            'icon_class' => '图标',
            'color' => '颜色值',
            'listorder' => '排序',
            'link' => '链接地址',
            'mark' => '备注',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getNavigation() {
        return $this->hasOne(Navigation::class, [
            'id' => 'navigation_id'
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getIcon() {
        if ($this->thumb) {
            return Url::attach($this->thumb);
        }
        return $this->icon_class;
    }

    /**
     * {@inheritdoc}
     */
    public function getBadge() {
        return 0;
    }

    /**
     * {@inheritdoc}
     */
    public function getMplink() {
        $link = $this->link;
        if (!$link) {
            return null;
        }
        return Url::mplink($this->link, [
            'title' => $this->title
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'title',
            'description',
            'name',
            'icon',
            'badge',
            'mplink',
        ];
    }

}
